package com.haochang.chunkvod.app.utils;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;

/**
 *     @author Yangjie
 *     className PackageManagerUtil
 *     created at  2016/9/26  11:09
 */
public class PackageManagerUtil
{
	private static volatile PackageManagerUtil singleton;

	public PackageManagerUtil()
	{
	}

	public static PackageManagerUtil getInstance()
	{
		if (null == singleton)
		{
			synchronized (PackageManagerUtil.class)
			{
				if (null == singleton)
				{
					singleton = new PackageManagerUtil();
				}
			}
		}
		return singleton;
	}

	/**
	 * 判断SD卡是否存在
	 */
	public boolean isSDcardExist()
	{
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}

	/**
	 * <pre>
	 * 卸载程序
	 * @param pkgName 包名 
	 * </pre>
	 */
	public void unInstallApp(Context context, String pkgName)
	{
		Intent intent = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + pkgName));
		context.startActivity(intent);
	}

	/**
	 * <pre>
	 *
	 * @param apkUrl 安装APP
	 *               <p>
	 *               <pre/>
	 */
	public void installApk(Context context, String apkUrl)
	{
		if (TextUtils.isEmpty(apkUrl))
		{
			return;
		}
		if (apkUrl.endsWith(".apk"))
		{
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			if (apkUrl.startsWith("file:"))
			{
				intent.setDataAndType(Uri.parse(apkUrl), "application/vnd.android.package-archive");
			} else
			{
				intent.setDataAndType(Uri.fromFile(new File(apkUrl)), "application/vnd.android.package-archive");
			}
			context.startActivity(intent);
		} else
		{
			// ToastFactory.getToast(context, "请到" + apkUrl + "解压安装！", true)
			// .show();
		}
	}

	/**
	 * 判断apk文件是否能安装
	 * @param filePath apk路劲
	 * @return apk是否完整可安装
	 */
	public boolean isApkCanInstall(Context mContext, String filePath)
	{
		try
		{
			PackageManager pm = mContext.getPackageManager();
			PackageInfo info = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
			return info != null;
		} catch (Exception e)
		{
			return false;
		}
	}

	/**
	 *获取指定路径中apk的版本code
	 * @param mContext
	 * @param filePath
	 * @return  -1为非完整apk文件
	 */
	public int getOtherApkVersionCode(Context mContext, String filePath)
	{
		try
		{
			PackageManager pm = mContext.getPackageManager();
			PackageInfo info = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
			if (info != null)
			{
				return info.versionCode;
			} else
			{
				return -1;
			}
		} catch (Exception e)
		{
			return -1;
		}
	}

	/**
	 *获取指定路径中apk的版本code
	 * @param filePath 已下载apk路径
	 * @return  包名
	 */
	public String getOtherApkPkgName(Context mContext, String filePath)
	{
		try
		{
			PackageManager pm = mContext.getPackageManager();
			PackageInfo info = pm.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
			if (info != null)
			{
				return info.packageName;
			} else
			{
				return "";
			}
		} catch (Exception e)
		{
			return "";
		}
	}

	public int getVersionCode(Context mContext)
	{
		try
		{
			PackageManager manager = mContext.getPackageManager();
			PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
			return info.versionCode;
		} catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 获取应用名称
	 */
	public String getAppName(Context context)
	{
		PackageManager packageManager = context.getPackageManager();
		try
		{
			CharSequence applicationLabel = packageManager.getApplicationLabel(packageManager.getApplicationInfo(context.getPackageName(), 0));
			return applicationLabel.toString();
		} catch (PackageManager.NameNotFoundException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 获取版本号
	 */
	public String getVersion(Context context)
	{
		PackageManager packageManager = context.getPackageManager();
		try
		{
			PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			return packInfo.versionName;
		} catch (PackageManager.NameNotFoundException e)
		{
			e.printStackTrace();
			return "1.0.0";
		}
	}
}
